package servlet;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;

import com.google.gson.Gson;

import pojo.Customer;
import pojo.PageBean;
import pojo.Product;
import service.CustomerService;
import service.ProductService;
import util.ListResponse;

/**
 * Servlet implementation class ProductServlet
 */
@WebServlet("/ProductServlet")
@MultipartConfig
public class ProductServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	String img;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProductServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		String method=request.getParameter("method");
		if(method.equals("showAll")) {
			showAll(request,response);
		}else if(method.equals("del")){
			del(request,response);
		}else if(method.equals("update")) {
			update(request,response);
		}else if(method.equals("up")) {
			up(request,response);
		}else if(method.equals("add")) {
			add(request,response);
		}
	}
	private void add(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		Product p =new Product();
		p.setP_type(request.getParameter("p_type"));
		p.setP_id(request.getParameter("p_id"));
		p.setP_name(request.getParameter("p_name"));
		p.setP_price(Float.parseFloat(request.getParameter("p_price")));
		p.setP_quantity(Integer.parseInt(request.getParameter("p_quantity")));
		try {
			Part part = request.getPart("p_image");
			String filename=part.getSubmittedFileName();//获取上传的文件名
			//创建image文件夹
			String path=this.getServletContext().getRealPath("image");
			File f = new File(path);
			if(!f.exists()) {
				f.mkdir();
			}
			String  name=UUID.randomUUID()+filename;
			img="image/"+name;
			part.write(path+"/"+name);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		p.setP_image(img);
		p.setP_description(request.getParameter("p_description"));
		p.setP_time(request.getParameter("p_time"));
		ProductService pservice = new ProductService();
		int i = pservice.add(p);
		if(i>0) {
			try {
				response.sendRedirect("ProductServlet?method=showAll&currentPage=1");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else {
			try {
				response.sendRedirect("fail.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
			}

	private void up(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String pid=request.getParameter("p_id");
		ProductService pservice = new ProductService();
		Product p = pservice.getProduct(pid);
		if(p!=null) {
			HttpSession session=request.getSession(true);
			session.setAttribute("P", p);
			try {
				response.sendRedirect("update.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	private void update(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String pid=request.getParameter("p_id");
		String name=request.getParameter("p_name");
		int quantity = Integer.parseInt(request.getParameter("p_quantity"));
		Part part;
		try {
			part = request.getPart("p_image");
			String filename=part.getSubmittedFileName();
			String str = this.getServletContext().getRealPath("image");
			File f=new File(str);
			if (!f.exists()){
				f.mkdir();
			}
		    String name1 = UUID.randomUUID()+filename;
		    img="image/"+name1;
			part.write(str+"/"+name1);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			
		} catch (ServletException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		ProductService pservice=new ProductService();
		Product p =new Product();
		p.setP_id(pid);
		p.setP_name(name);
		p.setP_quantity(quantity);
		p.setP_image(img);
		int i = pservice.update(p);
		if(i>0) {
			try {
				response.sendRedirect("http://localhost:8080/login/ProductServlet?method=showAll&currentPage=1");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else {
			try {
				response.sendRedirect("fail.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	private void del(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		
		
		
		
		String pid=request.getParameter("p_id");
		ProductService pservice = new ProductService();
		int flag = pservice.delProduct(pid);
		if(flag>0){
		try {
			response.sendRedirect("http://localhost:8080/login/ProductServlet?method=showAll&currentPage=1");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}else {
			try {
				response.sendRedirect("fail.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	private void showAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		String c=request.getParameter("currentPage");
		int currentPage;
		if(c==null) {
			currentPage=1;
		}else {
			currentPage=Integer.parseInt(c);
		}
		int pageSize = 10;
		ProductService ps=new ProductService();
		List<Product> pb=ps.showAll(currentPage, pageSize);
		System.out.println("servlet...."+currentPage+"  "+pageSize);
		if(pb!=null) {
			ListResponse<Product> lr=new ListResponse<Product>("200","操作成功",pb.size(),pb);
			String json = new Gson().toJson(lr);
			System.out.println(json);
			response.getWriter().write(json);
		}
		//分页显示  获取用户的请求数据
//		int currentPage =Integer.parseInt(request.getParameter("currentPage"));
//		int pageSize = 5;
//		//调用service方法
//		ProductService pservice = new ProductService();
//		PageBean<Product> pb= pservice.show( currentPage, pageSize);
//		request.setAttribute("pb", pb);
//		try {
//			request.getRequestDispatcher("showproduct.jsp").forward(request, response);
//		} catch (ServletException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}
}

